<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *挪车记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['code'])){
		$params['code']='%'.trim($_RQ['code']).'%';
		$return['params']['code']=trim($_RQ['code']);
	}
	if(!empty($_RQ['sn'])){
		$params['sn like']='%'.trim($_RQ['sn']).'%';
		$return['params']['sn']=trim($_RQ['sn']);
	}
	if(!empty($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_goodserpfw_falog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
			$item['kulog']=json_decode($item['kulog'],true);
			$item['desc']='内含'.count($item['kulog']).'条出库记录';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'出库信息'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'merchname','title'=>'经销商','type'=>'string'),
			array('name'=>'desc','title'=>'出库信息','type'=>'string'),
			array('type'=>'string','title'=>'快递公司','name'=>'expresstitle'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
			array('type'=>'string','title'=>'联系人','name'=>'realname'),
			array('type'=>'string','title'=>'联系电话','name'=>'mobile'),
			array('type'=>'string','title'=>'收货地址','name'=>'address'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'kulog','title'=>'出库记录'),
			array('type'=>'modal','op'=>'fa','title'=>'点击发货','show'=>array('status'=>0)),
			array('type'=>'post','op'=>'print','title'=>'打印'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>13,'title'=>'经销商','name'=>'merchid','action'=>'jy_merch/get_merch/'),
		)
	);
	$return['status']=array('待发货','已发货','已完成');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$res=$this->model('printmsg')->faprint($order_id);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败！','error');
	}
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['express'] || !$post['expresssn']){
			exi('请填写快递信息','error');
		}
		$post['expresstitle']=model('jy_weishop/model')->express_types()[$post['express']]['name'];		
		$this->update('falog',array('status'=>1,'express'=>$post['express'],'expresstitle'=>$post['expresstitle'],'expresssn'=>$post['expresssn']),array('id'=>intval($post['id']),'uniacid'=>$_SESSION['uniacid']));
		exi('发货成功！','','/ku.falog/');
	}
	$item=$this->get('falog',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'发货',
		'edit'=>array(
			array('title'=>'请选择发货快递','type'=>'tip'),
			array('type'=>'copy','title'=>'联系人','name'=>'realname'),
			array('type'=>'copy','title'=>'联系电话','name'=>'mobile'),
			array('type'=>'copy','title'=>'收货地址','name'=>'address'),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
		)
	);
	$return['express']=model('jy_weishop/model')->express_types();
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='kulog'){
	$falog=$this->get('falog',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$falog){
		exi('指定经销商不存在出库记录！','error');
	}
	$list=json_decode($falog['kulog'],true);
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'sn','title'=>'流水号','type'=>'string'),
			array('name'=>'code','title'=>'防伪码','type'=>'string'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'code_type','title'=>'类型','type'=>'radio','options'=>'code_type'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
	);
	$return['code_type']=$this->code_type;
	$return['status']=array('待生成发货单','已生成发货单');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_goodserpfw_kulog',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_goodserpfw_kulog',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']));
			}
		}
	}
	exi('删除成功！');
}